package com.dragon.flow.service.flowable.impl;

import cn.hutool.core.util.ObjectUtil;
import com.dragon.flow.constant.FlowablePlusConstant;
import com.dragon.flow.enm.flowable.ProcessInstanceStatusEnum;
import com.dragon.flow.service.flowable.BaseFlowableService;
import com.dragon.flow.service.flowable.FlowableProcessInstanceService;
import com.dragon.flow.vo.processInstance.PlusEndVo;
import com.dragon.flow.vo.processInstance.PlusInstanceQueryParamsVo;
import com.dragon.flow.vo.processInstance.PlusProcessInstanceVo;
import com.dragon.flow.vo.processInstance.PlusStartProcessInstanceVo;
import com.dragon.tools.common.ReturnCode;
import com.dragon.tools.pager.PagerModel;
import com.dragon.tools.pager.Query;
import com.dragon.tools.vo.ReturnVo;
import com.ruoyi.common.core.domain.entity.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.engine.runtime.ProcessInstanceBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @title:
 * @author: bruce.liu
 * @since: 2024/6/16 21:19
 */
@Slf4j
@Service
@Transactional
public class FlowableProcessInstanceServiceImpl extends BaseFlowableService implements FlowableProcessInstanceService {
    @Override
    public ReturnVo<PlusProcessInstanceVo> startProcessInstanceByKey(PlusStartProcessInstanceVo params) {
        ReturnVo<PlusProcessInstanceVo> returnVo = new ReturnVo<>(ReturnCode.FAIL, "发起失败");
        PlusProcessInstanceVo processInstanceVo = new PlusProcessInstanceVo();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey(params.getModelKey()).latestVersion().singleResult();
        if (processDefinition == null) {
            returnVo.setMsg("流程未发布，请确认!");
            return returnVo;
        }
        identityService.setAuthenticatedUserId(params.getUserCode());
        try {
            ProcessInstanceBuilder processInstanceBuilder = runtimeService.createProcessInstanceBuilder();
            if (StringUtils.isNotBlank(params.getMessageName())) {
                processInstanceBuilder.messageName(params.getMessageName().trim());
            } else {
                //设置自动跳过的唯一参数
                if (MapUtils.isNotEmpty(params.getVariables())) {
                    params.getVariables().put(FlowablePlusConstant.FLOWABLE_SKIP_EXPRESSION_ENABLED, true);
                }
                processInstanceBuilder.processDefinitionId(processDefinition.getId()).businessStatus(ProcessInstanceStatusEnum.APPLYING.getCode()).name(params.getFormName().trim()).overrideProcessDefinitionTenantId(params.getAppSn()).businessKey(params.getBusinessKey().trim()).variables(params.getVariables());
            }
            long s = System.currentTimeMillis();
            ProcessInstance processInstance = processInstanceBuilder.start();
            long e = System.currentTimeMillis();
            log.info("启动流程耗时:{}豪秒", (e - s));
            if (ObjectUtil.isNotEmpty(processInstance)) {
                BeanUtils.copyProperties(processInstance, processInstanceVo);
            }
            returnVo.setData(processInstanceVo);
            returnVo.setCode(ReturnCode.SUCCESS);
            returnVo.setMsg("发起成功");
        } finally {
            identityService.setAuthenticatedUserId(null);
        }
        return returnVo;
    }

    @Override
    public Map<String, Object> getStartVariables(PlusStartProcessInstanceVo params, SysUser personal) {
        return null;
    }

    @Override
    public PagerModel<ProcessInstance> findMyProcessinstancesPagerModel(PlusInstanceQueryParamsVo paramsVo, Query query) {
        return null;
    }

    @Override
    public ReturnVo<String> stopProcess(PlusEndVo endVo) {
        return null;
    }
}
